#include "SourceIO.h"

#include <itkBMPImageIO.h>

SourceIO::SourceIO(PipelineModel *pm)
{
	m_pipelineModel = pm;
	m_loadDataThread = NULL;
}

SourceIO::~SourceIO()
{

}

void SourceIO::loadDicomSeries(const QString &dirname, const QString &sourceLabel)
{
	if ( m_loadDataThread)
	{
		printf("[Warning]: Another thread is running");
		return;
	}

	// Test file type and create corresponding thread XXX
	m_currDataType = VTK_SHORT;
	m_currFileName = dirname;
	m_currSourceLabel = sourceLabel;

	switch ( m_currDataType)
	{
	case VTK_SHORT:
		createloadDicomSeriesThread<short>();
		break;
	case VTK_UNSIGNED_CHAR:
		createloadDicomSeriesThread<unsigned char>();
		break;
	case VTK_FLOAT:
		createloadDicomSeriesThread<float>();
		break;
	default:
		qDebug() << "[Warning]: Does not support file data type";
		break;
	}
}

void SourceIO::slotProgressDone()
{
	if ( m_loadDataThread)
	{
		switch(m_currDataType)
		{
		case VTK_SHORT:
			createBaseSourceFromThread<short>();
			break;
		case VTK_UNSIGNED_CHAR:
			createBaseSourceFromThread<unsigned char>();
			break;
		case VTK_FLOAT:
			createBaseSourceFromThread<float>();
			break;
		default:
			break;
		}
	}

	m_loadDataThread->wait();

	m_loadDataThread = NULL;

	emit emitProgressDone();
}

void SourceIO::write(Source *source)
{
	QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save"), "", tr("Image Files (*.vtk)"));
	if ( fileName.isEmpty())
		return;

	if ( source->type() == stVolume)
	{
		switch (source->dataType())
		{
		case VTK_UNSIGNED_CHAR:
			writeVolume<unsigned char>(source, fileName);
			break;
		case VTK_SHORT:
			writeVolume<short>(source, fileName);
			break;
		case VTK_FLOAT:
			writeVolume<float>(source, fileName);
			break;
		default:
			break;
		}
	}

}

void SourceIO::loadFile( const QString& filename, const QString& label)
{
	if ( m_loadDataThread)
	{
		printf("[Warning]: Another thread is running");
		return;
	}

	QString suffix = filename.right(3).toLower();

	if ( suffix == QString("vtk") || suffix == QString("dcm") || suffix == QString("mhd"))
	{
		//XXX Test file type and create corresponding thread XXX
		m_currDataType = VTK_SHORT;
		m_currFileName = filename;
		m_currSourceLabel = label;

		switch ( m_currDataType)
		{
		case VTK_SHORT:
			createloadVtkFileThread<short>();
			break;
		case VTK_UNSIGNED_CHAR:
			createloadVtkFileThread<unsigned char>();
			break;
		case VTK_FLOAT:
			createloadVtkFileThread<float>();
			break;
		default:
			qDebug() << "[Warning]: Does not support file data type";
			break;
		}
	}

	else if ( suffix == QString("bmp"))
	{

		typedef itk::BMPImageIO BMPImageIOType;
		BMPImageIOType::Pointer bmpIO = BMPImageIOType::New();

		bmpIO->SetFileName(filename.toLatin1().data());
		bmpIO->UpdateOutputData();

		bmpIO->Print(std::cout);

		//unsigned int compnum = bmpIO->GetNumberOfComponents();

		typedef itk::ImageFileReader<ImageUChar3D> ReaderType;
		ReaderType::Pointer reader = ReaderType::New();

		reader->SetFileName(filename.toLatin1().data());


		VolumeSource<ImageUChar3D> *source = 
			new VolumeSource<ImageUChar3D>(QString("%1").arg(label));

		source->setVolume(reader->GetOutput());
		source->setFileName(filename);
		m_pipelineModel->addSource(source);

		//std::cout << bmpIO->GetPixelTypeAsString() << std::endl;
	}
}